/*
 * Copyright (C) 2014 Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.bambole.controller.util;

import com.googlecode.bambole.domain.User;
import com.googlecode.bambole.facade.UserFacade;
import com.googlecode.bambole.util.PasswordUtil;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Eduardo Pereira Montenegro <epereiramontenegro@gmail.com>
 */
@Named("userUtil")
@SessionScoped
public class UserUtil implements Serializable {

    @EJB
    private UserFacade userFacade;

    @Inject
    private com.googlecode.bambole.controller.util.LocalizationUtil localizationUtil;

    @Inject
    private com.googlecode.bambole.controller.util.ThemeUtil themeUtil;

    private String emailAddress;
    private String password;

    public UserUtil() {

    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String login() throws Exception {

        User user = userFacade.getEnabledByEmailAddress(emailAddress);

        if (user != null) {

            if (PasswordUtil.isInputPasswordValid(password, user.getPassword())) {

                FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("user", user);

                user.setLastLogin(new Date());
                userFacade.edit(user);

                Locale userLocale = Locale.forLanguageTag(user.getLocale());
                localizationUtil.setSelected(userLocale);

                themeUtil.setSelected(user.getTheme());
                themeUtil.updateSelection();

                JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/bundle", userLocale)
                        .getString("LoginSuccessful"));

                return "/app/main.xhtml?faces-redirect=true";
            } else {
                JsfUtil.addErrorMessage(ResourceBundle.getBundle("/bundle", JsfUtil.getLocale()).getString("WrongPassword"));

                return null;
            }
        } else {
            JsfUtil.addErrorMessage(ResourceBundle.getBundle("/bundle", JsfUtil.getLocale()).getString("EmailAddressNotFound"));

            return null;
        }
    }

    public String logout() {

        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);

        if (session.getAttribute("user") != null) {

            session.invalidate();

            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/bundle", JsfUtil.getLocale())
                    .getString("LogoutSuccessful"));

        }

        return "/index.xhtml?faces-redirect=true";
    }

    public void sessionTimeout() throws IOException {
        logout();
        FacesContext.getCurrentInstance().getExternalContext().redirect("/bambole/faces/index.xhtml?faces-redirect=true");
    }
}
